package com.tone.ji_volunteer.mapper;

import com.tone.ji_volunteer.pojo.Activity;
import com.tone.ji_volunteer.pojo.Joiner;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author tone(ang)
 * version 1.0
 */
@Mapper
public interface ActivityMapper {
    List<Activity> list();
    Activity select(Integer id);
    List<Activity> founder(Integer userId);
    List<Activity> search(String query);
    @Insert("insert into ji_volunteer.images(id, url) VALUES (" +
            " #{activityID}, #{upload})")
    void addImages(String upload, int activityID);
    @Insert("insert into ji_volunteer.activity(user_id, title, create_time, end_time, province, city, region, detail, content, contact_info,state) " +
            "values (#{userId}, #{title}, #{createTime}, #{endTime}, #{province}, #{city}, #{region}, #{detail},#{content}, #{contactInfo},#{state})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 返回生成的主键
    void addActivity(Activity act);
    @Select("select url from ji_volunteer.images where id = #{activityId}")
    List<String> getImages(Integer activityId);
    @Insert("insert into ji_volunteer.participate(act_id, user_id) values (" +
            "#{activityId}, #{userId})")
    void join(Integer userId, Integer activityId);
    @Select("select act_id, user_id from ji_volunteer.participate where user_id = #{userId} and act_id = #{activityId}")
    Joiner isJoin(Integer userId, Integer activityId);

    List<Activity> participation(Integer userId);
    @Delete("delete from ji_volunteer.activity where id = #{id}")
    void delete(Integer id);
    @Delete("delete from ji_volunteer.images where id = #{id}")
    void deleteImage(Integer id);
    @Delete("delete from ji_volunteer.participate where act_id = #{id}")
    void deleteJoiner(Integer id);
    @Update("update ji_volunteer.activity set end_time = #{now} where id = #{activityId}")
    void endActivity(Integer activityId, LocalDateTime now);
}
